diff options
author | Mavlon <82092080+mavlushechka@users.noreply.github.com> | 2021-12-08 23:49:52 +0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-08 23:49:52 +0500 |
commit | b2a26abf48bc181d5390b6381e93a1a7faa155c6 (patch) | |
tree | 53a730799df2e92b51b0acbacb425cdfc9c10789 | |
parent | 58b6915be9b57beeeb36876b02b6d301a0f118cb (diff) |
Create README.md
-rw-r--r-- | README.md | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..bdcbd16 --- /dev/null +++ b/README.md @@ -0,0 +1,124 @@ +# StudentDatabase + +A website for comfortable collecting data about students. + +## Spring + +Project is based on Spring framework. + +### Aplication + +On start application checks if any roles in database, if there is not any, then creates `ADMIN` and `USER` roles. + +```java +@Autowired +private RoleRepository roleRepository; + +public static void main(String[] args) { + SpringApplication.run(Application.class, args); +} + +@Bean +CommandLineRunner init() { + return args -> { + saveRole("ADMIN"); + saveRole("USER"); + }; +} + +private void saveRole(String role) { + if (roleRepository.findByRole(role) == null) { + roleRepository.save(new Role(role)); + } +} +``` + +### CustomAuthenticationSuccessHandler + +If visitor is an user he will be redirected to home page, otherwise if visitor is an admin he will be redirected to admin panel. + +```java +@Override +public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { + response.setStatus(HttpServletResponse.SC_OK); + + for (GrantedAuthority auth : authentication.getAuthorities()) { + switch (auth.getAuthority()) { + case "USER" -> response.sendRedirect("/"); + case "ADMIN" -> response.sendRedirect("/admin-panel/information/students"); + } + } +} +``` + +### WebSecurityConfig + +Spring Security secures your password by `BCryptPasswordEncoder` method. + +```java +@Autowired +private BCryptPasswordEncoder bCryptPasswordEncoder; +@Autowired +CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler; + +@Bean +public UserDetailsService mongoUserDetails() { + return new CustomUserDetailsService(); +} + +@Override +protected void configure(AuthenticationManagerBuilder auth) throws Exception { + UserDetailsService userDetailsService = mongoUserDetails(); + auth + .userDetailsService(userDetailsService) + .passwordEncoder(bCryptPasswordEncoder); +} +``` + +### AdminPanelController + +Application creates student accounts by given arguments.\ +Example: 310-20 (`group`) and 10 (`number`). It will create 10 accounts with default login and password for 310-20 group students. + +```java +@GetMapping("/admin-panel/create-users") +public String createUsers(@RequestParam String group, @RequestParam int number, Map<String, Object> model) { + for (int i = 1; i <= number; i ++) { + Role userRole = roleRepository.findByRole("USER"); + User user = new User(i + "student" + group, i + "student" + group, true, new HashSet<>(List.of(userRole))); + User userFromDb = userRepository.findByUsername(user.getUsername()); + + if (userFromDb != null) { + model.put("message", "User exists!"); + return "/admin-panel/add-users"; + } + + userDetailsService.saveUser(user); + } + return "redirect:/admin-panel/all-users"; +} +``` + +## Gradle + +Project uses Gradle as build automation tool. + +Dependencies section in `build.gradle` file: +```gradle +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-data-mongodb:2.5.6' + implementation 'org.springframework.boot:spring-boot-starter-security:2.5.6' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf:2.5.6' + implementation 'org.springframework.boot:spring-boot-starter-web:2.5.6' + implementation 'org.springframework.boot:spring-boot-starter-tomcat:2.5.6' + testImplementation('org.springframework.boot:spring-boot-starter-test:2.5.6') { + exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' + } + testImplementation 'org.springframework.security:spring-security-test:5.5.2' +} +``` + +## Screenshots + + + |